#ifndef __M_LOGGER_H__
#define __M_LOGGER_H__
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


/*日志等级*/    
enum  {
    NORMAL, 
    DEBUG, 
    ERROR,
    FATAL
};

/*将日志等级转化为字符串*/
const char* level_to_stirng(int level) {
    switch (level)
    {
    case NORMAL:
        return "NORMAL";
    case DEBUG:
        return "DEBUG";
    case ERROR:
        return "ERROR";
    case FATAL:
        return "FATAL";
    default:
        return nullptr;
    }
}

#define LOG(level, format, ...) do {\
    const char* levelstr = level_to_stirng(level); /*日志等级*/\
    time_t ts = time(NULL);  /*时间戳*/\  
    struct tm *lt = localtime(&ts);  /*格式化时间*/\ 
    char buffer[32] = { 0 };\
    strftime(buffer, sizeof(buffer) - 1, "%y-%m-%d %H:%M:%S", lt);  /*格式化时间到字符串*/\
    char out_buffer[1024];\
    sprintf(out_buffer, "[%s][%s][%s:%d] " format "\n", levelstr, buffer, __FILE__, __LINE__, ##__VA_ARGS__);\
    write(1, out_buffer, strlen(out_buffer));\
} while(0)
#endif
